1   /*
2    * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.
8    *
9    * This code is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12   * version 2 for more details (a copy is included in the LICENSE file that
13   * accompanied this code).
14   *
15   * You should have received a copy of the GNU General Public License version
16   * 2 along with this work; if not, write to the Free Software Foundation,
17   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18   *
19   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20   * or visit www.oracle.com if you need additional information or have any
21   * questions.
22   */
23  
24  /*
25   * @test
26   * @bug 6771184
27   * @summary Some methods in text package don't throw BadLocationException when expected
28   * @author Pavel Porvatov
29   */
30  
31  import javax.swing.*;
32  import javax.swing.text.BadLocationException;
33  import javax.swing.text.Highlighter;
34  import javax.swing.text.JTextComponent;
35  import java.awt.*;
36  
37  public class bug6771184 {
38      public static void main(String[] args) {
39          SwingUtilities.invokeLater(new Runnable() {
40              public void run() {
41                  JTextArea textArea = new JTextArea("Tested string");
42  
43                  Highlighter highlighter = textArea.getHighlighter();
44                  Highlighter.HighlightPainter myPainter = new Highlighter.HighlightPainter() {
45                      public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c) {
46                      }
47                  };
48  
49                  int negativeTestedData[][] = {{50, 0},
50                          {-1, 1},
51                          {-5, -4},
52                          {Integer.MAX_VALUE, Integer.MIN_VALUE},
53                          {Integer.MIN_VALUE, Integer.MAX_VALUE},
54                          {Integer.MIN_VALUE, Integer.MIN_VALUE}};
55  
56                  for (int[] data : negativeTestedData) {
57                      try {
58                          highlighter.addHighlight(data[0], data[1], myPainter);
59  
60                          throw new RuntimeException("Method addHighlight() does not throw BadLocationException for (" +
61                                  data[0] + ", " + data[1] + ") ");
62                      } catch (BadLocationException e) {
63                          // Ok
64                      }
65  
66                      Object objRef;
67  
68                      try {
69                          objRef = highlighter.addHighlight(0, 1, myPainter);
70                      } catch (BadLocationException e) {
71                          throw new RuntimeException("highlighter.addHighlight(0, 1, myPainter) throws exception", e);
72                      }
73  
74                      try {
75                          highlighter.changeHighlight(objRef, data[0], data[1]);
76  
77                          throw new RuntimeException("Method changeHighlight() does not throw BadLocationException for (" +
78                                  data[0] + ", " + data[1] + ") ");
79                      } catch (BadLocationException e) {
80                          // Ok
81                      }
82                  }
83              }
84          });
85      }
86  }